package com.bawei.detail.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import com.bawei.common.entity.CommendEntity
import com.bawei.common.entity.ReplyCommendEntity
import com.bawei.detail.databinding.ItemCommendChildBinding
import com.bawei.detail.databinding.ItemCommendGroupBinding
import com.bawei.image.ImgOptions
import com.bawei.image.LoadImageUtil

/**
 * Author: Jason Zhang
 * Date: 2025/2/11
 * Desc:
 */
class CommendListAdapter constructor(private val list: MutableList<CommendEntity>): BaseExpandableListAdapter() {

    override fun getGroupCount(): Int = list.size

    override fun getGroup(groupPosition: Int): CommendEntity = list[groupPosition]

    override fun getGroupId(groupPosition: Int): Long = list[groupPosition].id.toLong()

    override fun getGroupView(groupPosition: Int,isExpanded: Boolean,convertView: View?, parent: ViewGroup?): View {
        var groupEntity = list[groupPosition]
        val groupDataBinding = ItemCommendGroupBinding.inflate(LayoutInflater.from(parent?.context), parent, false)

        LoadImageUtil.loadUrl(groupDataBinding.root.context, groupEntity.userlogo,  groupDataBinding.civUserIcon, ImgOptions.Builder.isCircle(true).build())
        groupDataBinding.tvNickName.text = if (groupEntity.nickname.isNullOrEmpty()) "小鱼爱猫" else groupEntity.nickname
        groupDataBinding.tvContent.text = groupEntity.content
        groupDataBinding.tvCreateTime.text = "发布时间：${groupEntity.createtime}"
        groupDataBinding.tvExpand.text = if (isExpanded) "——收起回复——" else "——展开回复——"

        return groupDataBinding.root
    }

    override fun getChildrenCount(groupPosition: Int): Int = list[groupPosition].replytotal

    override fun getChild(groupPosition: Int, childPosition: Int): ReplyCommendEntity = list[groupPosition].replyList[childPosition]

    override fun getChildId(groupPosition: Int, childPosition: Int): Long = list[groupPosition].replyList[childPosition].id.toLong()

    override fun hasStableIds(): Boolean = true

    override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup?): View {
        val childEntity = list[groupPosition].replyList[childPosition]
        val childDataBinding = ItemCommendChildBinding.inflate(LayoutInflater.from(parent?.context), parent, false)
        LoadImageUtil.loadUrl(childDataBinding.root.context, childEntity.userlogo,  childDataBinding.civUserIcon, ImgOptions.Builder.isCircle(true).build())
        childDataBinding.tvNickName.text = if(childEntity.nickname.isNullOrEmpty()) "爱哭的小女孩" else childEntity.nickname
        childDataBinding.tvContent.text = childEntity.content
        childDataBinding.tvReplyTime.text = "回复时间:${childEntity.createtime}"
        return childDataBinding.root

    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean = true


}